package per.turtle.system.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import per.turtle.common.core.annotation.validate.MaxSizeWithCharset;
import per.turtle.common.core.constant.UserConstants;
import per.turtle.common.core.web.entity.BaseEntity;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @author turtle
 */
@Schema(name = "SysRole", description = "角色")
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@TableName("sys_role")
public class SysRole extends BaseEntity {

    private static final long serialVersionUID = 1L;

    @Schema(description = "角色ID")
    @TableId(type = IdType.ASSIGN_UUID)
    private String roleId;

    @Schema(description = "角色名称")
    @NotBlank(message = "角色名称不能为空")
    @MaxSizeWithCharset(max = 31, message = "角色名称超出限定长度！")
    private String roleName;

    @Schema(description = "角色权限")
    @NotBlank(message = "权限字符不能为空")
    @MaxSizeWithCharset(max = 63, message = "权限字符超出限定长度！")
    private String roleKey;

    @Schema(description = "显示顺序")
    @Max(value = 9999, message = "显示顺序取值范围在4位数以内")
    @Min(value = 1, message = "显示顺序取值需大于0")
    @NotNull(message = "显示顺序不能为空")
    @OrderBy(asc = true)
    private Integer orderNum;

    @Schema(description = "数据范围（1：所有数据权限；2：自定义数据权限；3：本部门数据权限；4：本部门及以下数据权限；5：仅本人数据权限）")
    private Integer dataScope;

    @Schema(description = "角色状态（0：正常；1：停用）")
    private Boolean isDisabled;

    @Schema(description = "用户是否存在此角色标识 默认不存在")
    @TableField(exist = false)
    private Boolean flag = false;

    @Schema(description = "菜单组")
    @TableField(exist = false)
    private String[] menuIds;

    @Schema(description = "部门组（数据权限）")
    @TableField(exist = false)
    private String[] deptIds;

    public SysRole(String roleId) {
        this.roleId = roleId;
    }

    public static boolean isAdmin(String roleId) {
        return UserConstants.ADMIN_ROLE_ID.equals(roleId);
    }

    public boolean isAdmin() {
        return isAdmin(this.roleId);
    }

}
